package com.tang.thepath;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.view.ImageUploader;

import java.util.ArrayList;

public class SoldInfoActivity extends BaseActivity implements View.OnClickListener{

    private EditText editName;
    private EditText editInformation;
    private TextView tvLnglat;
    private Button btnLocation;
    private Button btnSave;
    private ImageUploader imageUploader;
    private boolean registedReceiver = false;

    private double lng;
    private double lat;


    private BroadcastReceiver locationReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            double lat = intent.getDoubleExtra("lat",0);
            double lng = intent.getDoubleExtra("lng",0);
            Log.i(getTag(),"获得定位:"+lat+", "+lng);
            tvLnglat.setText(lat+","+lng);
            unregisterReceiver();
            dismisLoading();
            if(!Const.REAL_UPLOADING){
                stopService(new Intent(getContext(),UploadLocationService.class));
            }

        }
    };

    @Override
    public ImageUploader getImageUploader() {
        return imageUploader;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sold_info);

        editName = (EditText) findViewById(R.id.edit_name);
        editInformation = (EditText) findViewById(R.id.edit_howmany);
        tvLnglat = (TextView) findViewById(R.id.tv_lnglat);
        btnLocation = (Button) findViewById(R.id.btn_location);
        btnSave = (Button) findViewById(R.id.btn_save);
        imageUploader = (ImageUploader) findViewById(R.id.img_uploader);

        btnLocation.setOnClickListener(this);
        btnSave.setOnClickListener(this);

    }

    protected void registerReceiver(){
        if(!registedReceiver){
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(UploadLocationService.BROADCAST_REQUEST_LOCATION_ACTION);
            registerReceiver(locationReceiver,intentFilter);
            registedReceiver = true;
        }

    }

    protected void unregisterReceiver() {
        if (registedReceiver) {
            unregisterReceiver(locationReceiver);
            registedReceiver = false;
        }
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_location:
                showLoading("正在定位");
                registerReceiver();
                Intent intent = new Intent(getContext(),UploadLocationService.class);
                intent.putExtra("provider","gps");
                startService(intent);
                break;
            case R.id.btn_save:
                onBtnSaveClick(view);
                break;
        }
    }

    protected void onBtnSaveClick(View view){
        if(Const.NETWORK_MOD == 0){
            toast("请检查您的网络设置");
            return;
        }
        String name = editName.getText().toString();
        String information = editInformation.getText().toString();

        if(name.isEmpty()){
            toast("客户名称不能为空");
            return;
        }
        if(information.isEmpty()){
            toast("描述信息不能为空");
            return;
        }

        /*if(lat == 0 || lng == 0){
            toast("");
            return;
        }*/

        final RequestModel.StoreSoldInfoRequest request = new RequestModel.StoreSoldInfoRequest();
        request.customId = 0;
        request.name = name;
        request.information = information;
        request.latitude = lat;
        request.longitude = lng;

        //上传图片
        if(imageUploader.imageCount() > 0){
            showLoading("正在上传图片");
            imageUploader.uploadImages().subscribe(new HttpSubscriber<RequestModel.UploadResponse>() {
                @Override
                protected void onSuccess(RequestModel.UploadResponse uploadResponse) {
                    dismisLoading();
                    request.images = uploadResponse.pathList;
                    uploadSoldInfo(request);
                }
            });
        }else{
            request.images = new ArrayList<>();
            uploadSoldInfo(request);
        }

    }

    protected void uploadSoldInfo(RequestModel.StoreSoldInfoRequest request){
        showLoading("数据提交中");
        BizService.storeSoldInfo(request).subscribe(new HttpSubscriber<RequestModel.StoreSoldInfoResponse>() {
            @Override
            protected void onSuccess(RequestModel.StoreSoldInfoResponse storeSoldInfoResponse) {
                String message = "存储成功:"+storeSoldInfoResponse.id;
                Log.i(getTag(),message);
                toast(message);
                dismisLoading();
                finish();
            }
        });
    }

}
